/**  

* @Title: LoginAction.java 

* @Package com.gxbadge.action 

* @Description: TODO(用一句话描述该文件做什么) 

* @author CST

* @version V1.0  

*/ 

package com.gxbadge.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.gxbadge.action.base.BaseAction;
import com.gxbadge.model.Administrator;
import com.gxbadge.model.Category;
import com.gxbadge.model.Language;
import com.gxbadge.model.Navigation;
import com.gxbadge.model.Operator;
import com.gxbadge.util.MD5;
import com.opensymphony.xwork2.ActionContext;

public class CategoryAction extends BaseAction {
	
	private Category cate;
	private List<Category> cateList;
	private Map<Category, List<Category>> cateMap;
	public Category getCate() {
		return cate;
	}
	public void setCate(Category cate) {
		this.cate = cate;
	}
	public List<Category> getCateList() {
		return cateList;
	}
	public void setCateList(List<Category> cateList) {
		this.cateList = cateList;
	}
	public Map<Category, List<Category>> getCateMap() {
		return cateMap;
	}
	public void setCateMap(Map<Category, List<Category>> cateMap) {
		this.cateMap = cateMap;
	}
	
	private Navigation nav;
	private List<Navigation> navList;
	private Map<Navigation, List<Navigation>> navMap;

	public Navigation getNav() {
		return nav;
	}
	public void setNav(Navigation nav) {
		this.nav = nav;
	}
	public List<Navigation> getNavList() {
		return navList;
	}
	public void setNavList(List<Navigation> navList) {
		this.navList = navList;
	}
	public Map<Navigation, List<Navigation>> getNavMap() {
		return navMap;
	}
	public void setNavMap(Map<Navigation, List<Navigation>> navMap) {
		this.navMap = navMap;
	}
	
	
	
	public String top_cate() throws Exception{
		
		List<Category> cateList = adminManager.findCateListByCatePid(0);
		if(cateList!=null){
			this.setCateList(cateList);
		}
		return "cate_top";
	}
	
	public String left_cate() throws Exception {
		
		if(this.getCateList()!=null){
			this.setCateList(null);
		}
		if(this.getCateMap()!=null){
			this.setCateMap(null);
		}
		if(this.getCate()!=null&&this.getCate().getCateId()!=null){

			Category cate = adminManager.findCateByCateId(this.getCate().getCateId());
			if(cate!=null){
				
				Map<Category, List<Category>> cateMap = new LinkedHashMap<Category, List<Category>>();
				this.setCate(cate);
				List<Category> cateList = adminManager.findCateListByCatePid(cate.getCateId());
				if(cateList!=null&&cateList.size()>=1){
					cateMap.put(cate, cateList);
				}
				if(cateMap!=null&&cateMap.size()>=1){
					this.setCateMap(cateMap);
				}
				this.setCate(null);
				
			}
		}else{
			
			List<Category> cateList = adminManager.findCateListByCatePid(0);
			if(cateList!=null){
				
				this.setCateList(cateList);
				this.setCate(null);
			}
		}
		

		return "cate_left";
	}
	
	private String paramCateId;
	
	public String getParamCateId() {
		return paramCateId;
	}
	public void setParamCateId(String paramCateId) {
		this.paramCateId = paramCateId;
	}
	
	private Map<Category, List<Category>> cateLev2Map;
	
	public Map<Category, List<Category>> getCateLev2Map() {
		return cateLev2Map;
	}
	public void setCateLev2Map(Map<Category, List<Category>> cateLev2Map) {
		this.cateLev2Map = cateLev2Map;
	}
	public String find_cateSystem() throws Exception{
		
		
		if(this.getParamCateId()!=null){
			
			
			if(this.getCateLev2Map()!=null){
				this.setCateLev2Map(null);
			}
			int cateId = Integer.parseInt(this.getParamCateId());
			List<Category> cateList = adminManager.findCateListByCatePid(cateId);
			if(cateList!=null&&cateList.size()>=1){
				
				Map<Category, List<Category>> cMap = new LinkedHashMap<Category, List<Category>>();
				for(int i=0;i<cateList.size();i++){
					
					List<Category> cList = adminManager.findCateListByCatePid(cateList.get(i).getCateId());
					if(cList!=null&&cList.size()>=1){
						cMap.put(cateList.get(i), cList);
						
					}else{	
						
						cMap.put(cateList.get(i), null);
					}
					
				}
				if(cMap!=null&&cMap.size()>=1){
					this.setCateLev2Map(cMap);
					
				}
				
			}
			this.setCate(null);
			this.setParamCateId(null);
		}else{
			
			List<Category> cateList = adminManager.findCateListByCatePid(0);
			if(cateList!=null){
				
				Map<Category, List<Category>> cMap = new LinkedHashMap<Category, List<Category>>();
				this.setCateList(cateList);
				
				for(int i=0;i<cateList.size();i++){
					
					List<Category> cList = adminManager.findCateListByCatePid(cateList.get(i).getCateId());
					
					if(cList!=null&&cList.size()>=1){
						
						cMap.put(cateList.get(i), cList);
					}else{
						
						cMap.put(cateList.get(i), null);
					}		
				}
				if(cMap!=null&&cMap.size()>=1){
					this.setCateMap(cMap);
					cMap = null;
				}
			}
			this.setCate(null);
		}
		
		return "cateSystem_find_success";
	}
	
	//循环遍历某分类模块下所有分类模块
	public List<Category> cycleCateList(Integer catePid){
		
		List<Category> csList = new LinkedList<Category>();
		List<Category> cateList = adminManager.findCateListByCatePid(catePid);
		if(cateList!=null&&cateList.size()>=1){

			for(int i=0;i<cateList.size();i++){
				csList.add(cateList.get(i));
				List<Category> cList = this.cycleCateList(cateList.get(i).getCateId());
				if(cList!=null&&cList.size()>=1){
					csList.addAll(cList);
				}
				
			}
		}

		return csList;
	}
	
	
	private String[] parentId;
	
	public String[] getParentId() {
		return parentId;
	}
	public void setParentId(String[] parentId) {
		this.parentId = parentId;
	}
	
	private Language lang;
	private List<Language> langList;
	private String[] selectLangId;
	public Language getLang() {
		return lang;
	}
	public void setLang(Language lang) {
		this.lang = lang;
	}
	public List<Language> getLangList() {
		return langList;
	}
	public void setLangList(List<Language> langList) {
		this.langList = langList;
	}
	
	public String[] getSelectLangId() {
		return selectLangId;
	}
	public void setSelectLangId(String[] selectLangId) {
		this.selectLangId = selectLangId;
	}
	public String insert_cate() throws Exception{
		
		if(this.getCate()!=null&&this.getCate().getCateId()!=null){
			
			
			Category cate = adminManager.findCateByCateId(this.getCate().getCateId());
			if(cate!=null){
				this.setCate(cate);
				List<Category> cateList = this.cycleCateList(0);
				if(cateList!=null&&cateList.size()>=1){
					this.setCateList(cateList);
				}
				List<Language> langList = adminManager.findLangAll();
				if(langList!=null){
					this.setLangList(langList);
				}
			}
		}

		return "cate_insert_success";
	}
	
	public String add_cate() throws Exception{
		
		if(this.getParentId()!=null){
			int catePid = Integer.parseInt(this.getParentId()[0]);
			
			Category cate = new Category();
			
			if(this.getSelectLangId()!=null){
				int langId = Integer.parseInt(this.getSelectLangId()[0]);
				Language lang = adminManager.findLangByLangId(langId);
				cate.setLang(lang);
			}else{
				Language lang = this.getCate().getLang();
				cate.setLang(lang);
			}
			
			cate.setCatePid(catePid);
			cate.setTitle(this.getCate().getTitle());
			cate.setUrl(this.getCate().getUrl());
			if(catePid>0){
				Category c = adminManager.findCateByCateId(Integer.parseInt(this.getParentId()[0]));
				byte levFlag = (byte) (c.getLevFlag()+1);
				cate.setLevFlag(levFlag);
			}else{
				byte levFlag = 0;
				cate.setLevFlag(levFlag);
			}
			adminManager.add_cate(cate);
		}
		this.setParentId(null);
		return "cate_add_success";
	}
	public void cycleDel(int cateId){
		
		Category cate = adminManager.findCateByCateId(cateId);
		if(cate!=null){
			adminManager.del_cate(cate);
		}
		List<Category> cateList = adminManager.findCateListByCatePid(cateId);
		if(cateList!=null&&cateList.size()>=1){
			for(int i=0;i<cateList.size();i++){
				this.cycleDel(cateList.get(i).getCateId());
			}
		}		
	}
	public String del_cate() throws Exception{
		
		if(this.getCate()!=null&&this.getCate().getCateId()!=null){
			this.cycleDel(this.getCate().getCateId());
		}
		return "cate_del_success";
	}
	
	public String upload_cate() throws Exception{
		
		if(this.getCate()!=null&&this.getCate().getCateId()!=null){
			
			Category cate = adminManager.findCateByCateId(this.getCate().getCateId());
			if(cate!=null){
				this.setCate(cate);
				List<Category> cateList = this.cycleCateList(0);
				if(cateList!=null&&cateList.size()>=1){
					this.setCateList(cateList);
				}
			}
		}
		return "cate_upload_success";
		
	}
	
	public String modify_cate() throws Exception {
		
		if(this.getParentId()!=null){
			int catePid = Integer.parseInt(this.getParentId()[0]);
			if(catePid!=this.getCate().getCateId()){
				this.getCate().setCatePid(catePid);
				
				/*
				 * levFlag设置
				 */
				
				
			}
			adminManager.modify_cate(this.getCate());
		}
		this.setParentId(null);
		return "cate_modify_success";
	}
	private String paramId;
	private List<Operator> operList;
	private Map<Navigation, List<Operator>> navOperMap;
	
	public String getParamId() {
		return paramId;
	}
	public void setParamId(String paramId) {
		this.paramId = paramId;
	}
	
	public Map<Navigation, List<Operator>> getNavOperMap() {
		return navOperMap;
	}
	public void setNavOperMap(Map<Navigation, List<Operator>> navOperMap) {
		this.navOperMap = navOperMap;
	}
	public List<Operator> getOperList() {
		return operList;
	}
	public void setOperList(List<Operator> operList) {
		this.operList = operList;
	}
	public String website_cate() throws Exception{
		
		if(this.getParamId()!=null){
			
			
			if(this.getNavOperMap()!=null){
				this.setNavOperMap(null);
			}
			
			int navId = Integer.parseInt(this.getParamId());
			Navigation nav = adminManager.findNavByNavId(navId);
			if(nav!=null){
				
				Map<Navigation, List<Operator>> navOperMap = new LinkedHashMap<Navigation, List<Operator>>();
				
				List<Operator> operList = adminManager.findOperListByNav(nav);
				if(operList!=null&&operList.size()>=1){
					
					navOperMap.put(nav, operList);
				}else{
					navOperMap.put(nav, null);
				}
				if(navOperMap!=null&&navOperMap.size()>=1){
					this.setNavOperMap(navOperMap);
				}
			}
			
		}else{
			
			Map<Navigation, List<Navigation>> navMap = new LinkedHashMap<Navigation, List<Navigation>>();
			List<Navigation> navList = adminManager.findNavListByNavPid(0);
			if(navList!=null&&navList.size()>=1){
				for(int i=0;i<navList.size();i++){
					List<Navigation> nList = adminManager.findNavListByNavPid(navList.get(i).getNavId());
					if(nList!=null&&nList.size()>=1){
						
						navMap.put(navList.get(i), nList);
					
					}else{
						navMap.put(navList.get(i), null);
					}
				}
				if(navMap!=null&&navMap.size()>=1){
					this.setNavMap(navMap);
				}
			}
			
		}
		return "cate_website_success";
	}
	
	//网站导航管理模块
	public String find_cate_navList() throws Exception{
		
		if(this.getCateList()!=null){
			this.setCateList(null);
		}
		if(this.getCateMap()!=null){
			this.setCateMap(null);
		}
		Category cate = adminManager.findCateByNote("site");
		if(cate!=null){
			this.setCate(cate);
			List<Category> cateList = adminManager.findCateListByCatePid(cate.getCateId());
			if(cateList!=null&&cateList.size()>=1){
				this.setCateList(cateList);
			}
		}
		
		
		return "cate_navList_find_success";
	}
	
	
	
}
